package com.clwl.supervise.superviseapp.controller.inspection;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clwl.supervise.superviseapp.config.RepeatSubmit;
import com.clwl.supervise.superviseapp.entity.base.CommonSearchEntity;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.entity.supervise.JgFlightContent;
import com.clwl.supervise.superviseapp.entity.supervise.JgFlightPlanCheck;
import com.clwl.supervise.superviseapp.entity.supervise.JgFlightPlanUser;
import com.clwl.supervise.superviseapp.service.inspect.FlightPlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 飞行检查
 */
@Slf4j
@RestController
@RequestMapping("flightPlan")
public class FlightPlanController {


    /**
     * 体系检查列表信息
     * @param pageNum
     * @param pageSize
     * @param params 请求参数
     * @return
     */
    @GetMapping("getList")
    private ResultEntity<IPage<Map<String,Object>>> getCompanyList(@RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize, CommonSearchEntity params){

        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);

        IPage<Map<String,Object>> pageList = flightPlanService.selectCompanyPage(pageNum,pageSize,params);
        return ResultEntity.success(pageList);
    }

    /**
     * 体系检查列表信息
     * @param checkId  检查单id
     * @return
     */
    @GetMapping("schemeCheckInfo")
    @RepeatSubmit
    private ResultEntity<JgFlightPlanCheck> schemeCheckInfo(Long checkId){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            JgFlightPlanCheck checkInfo = flightPlanService.getSchemeCheckInfo(checkId);
            return ResultEntity.success(checkInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取信息失败");
        }
    }
    /**
     * 体系检查人员列表
     * @return
     */
    @GetMapping("getFlightSupervisePersonList")
    public ResultEntity<List<JgFlightPlanUser>> getFlightSupervisePersonList(Long plantId){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            List<JgFlightPlanUser> userList  = flightPlanService.getFlightSupervisePersonList(plantId);
            return ResultEntity.success(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取信息失败");
        }
    }

    /**
     * 保存检查门头照照片
     * @param checkId 检查Id
     * @param file 门头照文件
     * @return
     */
    @PostMapping("uploadCheckFile")
    public ResultEntity uploadCheckFile(Long checkId, MultipartFile file){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            JgFlightPlanCheck check =  flightPlanService.uploadCheckFile( checkId, file);
            return ResultEntity.success(check);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 保存告知单
     * @param checkId
     * @param informFile
     * @return
     */
    @PostMapping("saveCheckInform")
    public ResultEntity saveCheckInform(Long checkId, String  informFile ){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            String  filePath = flightPlanService.saveCheckInform(checkId,   informFile);
            return ResultEntity.success(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }



    /**
     * 保存/临时保存检查项结果内容
     * @param temporary 检查id
     * @param checkData 联系电话
     * @return
     */
    @PostMapping("saveCheckInfo")
    @RepeatSubmit(lockTime = 10)
    public ResultEntity saveCheckInfo(String temporary,  String checkData ){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            JgFlightPlanCheck flightPlanCheck = JSONObject.parseObject(checkData, JgFlightPlanCheck.class);
            JgFlightPlanCheck dailyCheck =  flightPlanService.saveCheckInfo(temporary,flightPlanCheck);
            return ResultEntity.success(dailyCheck);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存/临时保存检查项结果内容失败");
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 最终保存
     * @param checkId
     * @return
     */
    @PostMapping("finaSaveInfo")
    @RepeatSubmit(lockTime = 10)
    public ResultEntity finaSaveInfo(Long checkId){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            JgFlightPlanCheck dailyCheck =  flightPlanService.finaSaveInfo(checkId);
            return ResultEntity.success(dailyCheck);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存/临时保存检查项结果内容失败");
            return ResultEntity.error("操作失败");
        }
    }


    /**
     * 上传结果记录的文件
     * @param checkId 日常检查结果表id
     * @param imgFile 二进制文件流
     * @param type 1-要点表；2-记录表
     * @return
     */
    @PostMapping("uploadResultRecord")
    public ResultEntity uploadResultRecord(Long checkId,@RequestPart(required = true,name = "imgFile") MultipartFile imgFile,String type
    ){
        try {
            if (imgFile != null && StringUtils.isBlank(imgFile.getOriginalFilename())) {
                return ResultEntity.error("请上传文件");
            }
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            String fileUrl = flightPlanService.uploadResultRecord(checkId,imgFile,type);
            return ResultEntity.success(fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上传结果记录的文件失败");
            return ResultEntity.error("操作失败");
        }
    }





    /**
     * 查询检查内容的列表
     * @param checkId 日常检查表JG_DAILY_CHECK  id
     * @return
     */
    @GetMapping("getCheckInfo")
    public ResultEntity getCheckInfo(Long checkId){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            JgFlightPlanCheck checkInfo = flightPlanService.getCheckInfo(checkId);
            return ResultEntity.success(checkInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询检查信息失败");
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 查询结果表需要的数据
     * @param checkId
     * @return
     */
    @GetMapping("getResultTable")
    public ResultEntity getResultTable(Long checkId){
        try {
            FlightPlanService dailyInspectService = SpringUtil.getBean(FlightPlanService.class);
            Map<String,Object> resultTable = dailyInspectService.getResultTable(checkId);
            return ResultEntity.success(resultTable);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询检查信息失败");
            return ResultEntity.error("操作失败");
        }
    }


    /**
     * 查询检查完成的订单
     * @param checkId
     * @return
     */
    @GetMapping("getCheckResultInfo")
    public ResultEntity getCheckResultInfo(Long checkId){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            Map<String, Object> result = flightPlanService.getCheckResultInfo(checkId);
            return ResultEntity.success(result);
        } catch (Exception e) {
            log.error("查询检查完成的订单失败");
            return ResultEntity.error("操作失败");
        }
    }


    /**
     * 查询检查内容的列表
     * @param standardId
     * @return
     */
    @GetMapping("getCheckContentList")
    public ResultEntity getCheckContentList(Long standardId){
        try {
            FlightPlanService flightPlanService = SpringUtil.getBean(FlightPlanService.class);
            List<JgFlightContent> contentList = flightPlanService.getCheckContentList(standardId);
            return ResultEntity.success(contentList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询检查内容的列表失败");
            return ResultEntity.error("操作失败");
        }
    }



}
